﻿Imports System.Windows
Imports System.Windows.Media

Public Class cpb
    Public Sub New()

        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        RenderArc()
    End Sub
    Dim _Percentage As Double = 65.0
    Public Property Percentage As Double
        Get
            Return _Percentage
        End Get
        Set(ByVal value As Double)
            _Percentage = value
            RenderArc()
        End Set
    End Property

    Dim _StringMuet As String
    Public Property StringMuet As String
        Get
            Return _StringMuet
        End Get
        Set(ByVal value As String)
            _StringMuet = value.ToUpper
        End Set
    End Property
    Public Sub RenderArc()
        Dim _Radius As Integer = 31
        Dim startPoint As New Point(_Radius, 0)

        ' volume
        Dim _Angle As Double = (_Percentage * 360) / 200
        Dim endPoint As Point = ComputeCartesianCoordinate(_Angle, _Radius)
        endPoint.X += _Radius
        endPoint.Y += _Radius

        pathRootVolume.Width = _Radius * 2 + pathRootVolume.StrokeThickness
        pathRootVolume.Height = _Radius * 2 + pathRootVolume.StrokeThickness
        pathRootVolume.Margin = New Thickness(pathRootVolume.StrokeThickness, pathRootVolume.StrokeThickness, 0, 0)

        Dim largeArc As Boolean = _Angle > 180.0

        Dim outerArcSize As New Size(_Radius, _Radius)

        pathFigureVolume.StartPoint = startPoint

        If startPoint.X = Math.Round(endPoint.X) AndAlso startPoint.Y = Math.Round(endPoint.Y) Then
            endPoint.X -= 0.01
        End If

        arcSegmentVolume.Point = endPoint
        arcSegmentVolume.Size = outerArcSize
        arcSegmentVolume.IsLargeArc = largeArc

        ' fond
        _Angle = 360
        endPoint.X = 30.99
        endPoint.Y = 0
        pathRootFond.Width = _Radius * 2 + pathRootFond.StrokeThickness
        pathRootFond.Height = _Radius * 2 + pathRootFond.StrokeThickness
        pathRootFond.Margin = New Thickness(pathRootFond.StrokeThickness, pathRootFond.StrokeThickness, 0, 0)
        pathFigureFond.StartPoint = startPoint
        arcSegmentFond.Point = endPoint
        arcSegmentFond.Size = outerArcSize
        arcSegmentFond.IsLargeArc = True

        'label
        If _Percentage > 0 Then
            label1.Content = _Percentage.ToString()
        Else
            label1.Content = _StringMuet
        End If

    End Sub
    Private Function ComputeCartesianCoordinate(ByVal angle As Double, ByVal radius As Double) As Point
        ' convert to radians
        Dim angleRad As Double = (Math.PI / 180.0) * (angle - 90)
        Dim x As Double = radius * Math.Cos(angleRad)
        Dim y As Double = radius * Math.Sin(angleRad)

        Return New Point(x, y)
    End Function
    Public Sub Scale(ByVal cote As Double)
        Dim ratio As Double = cote / 96
        Dim scaler As New ScaleTransform(ratio, ratio, MainGrid.ActualHeight / 2, MainGrid.ActualWidth / 2)
        Me.LayoutTransform = scaler
        Me.UpdateLayout()
    End Sub
End Class
